<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="编号，主键自增" align="center" prop="id" /> -->
      <el-table-column label="外部sku编码" align="center" prop="organizationProductSku" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <!-- <el-table-column label="商品价格" align="center" prop="productPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.productPrice)  }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="单个商品重量" align="center" prop="productWeight" />
      <el-table-column label="单个商品单位" align="center" prop="productUnit" /> -->
      <el-table-column align="center" label="规格" prop="productWeight">
        <template #default="scope">
          {{ scope.row.specs  }}
          <!-- <dict-tag :type="DICT_TYPE.PRODUCT_PRODUCT_UNIT" :value="scope.row.productUnit" /> -->
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="productPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.productPrice)  }}
        </template>
      </el-table-column>
      <el-table-column label="系数" align="center" prop="jinCount" />
      <el-table-column label="原材料价格" align="center" prop="rawMaterialPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.rawMaterialPrice)  }}
        </template>
      </el-table-column>
      <el-table-column label="出成率" align="center" prop="useRatio">
        <template #default="scope">
          {{ percentageFormatter(scope.row.useRatio)  }}
        </template>
      </el-table-column>
      <el-table-column label="原材料价格" align="center" prop="jinMaterialPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.jinMaterialPrice)  }}
        </template>
      </el-table-column>
      <el-table-column label="加工费" align="center" prop="processPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.processPrice)  }}
        </template>
      </el-table-column>
      <el-table-column label="运输费" align="center" prop="transportRatio">
        <template #default="scope">
          {{ percentageFormatter(scope.row.transportRatio)  }}
        </template>
      </el-table-column>
      <el-table-column label="净菜价格" align="center" prop="jinPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.jinPrice)  }}
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="sumPrice">
        <template #default="scope">
          {{ priceFormatter(scope.row.sumPrice)  }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            :loading="exportLoading"
            @click="handleExport(scope.row.id)"
            v-hasPermi="['quotation:quotation:export']"
          >
            导出
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
        <!-- 分页 -->
    <!-- <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </ContentWrap>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { Api, ProductVO } from '@/api/quotation/quotation'
import download from '@/utils/download'


const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  quotationId: undefined // 报价配置ID（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref<ProductVO[]>([])  // 列表的数据
const exportLoading = ref(false) // 导出的加载中

// const total = ref(0)
// const queryParams = reactive({
//   pageNo: 1,
//   pageSize: 10,
//   quotationId: undefined,
// })

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await Api.getProductPage({ quotationId: props.quotationId })
    list.value = data.list
    // total.value = data.total
  } finally {
    loading.value = false
  }
}

const priceFormatter = (price) => { 
  return (price/100).toFixed(2)
}

const percentageFormatter = (proportion) => { 
  return proportion*100+'%'
}


/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await Api.exportProduct({ quotationId: props.quotationId })
    console.log(data)
    download.excel(data, '商品报价.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>